/*	game.c 
 *  Arizona State University
 *  CSE 325: Embedded Systems Programming
 *  Lab 5: Game of life
 *  Georgii Tkachuk, Brentton Garber
 *   Emails: 	gtkachuk@asu.edu btgarber@asu.edu
 * 	Description: Game module is responsible for the LED matrix initialization. It holds the software that calculates the patterns for the next matrix in the cycle. 
 * 				 
 * 
 */

#include "common.h"
int the_matrix[8][8];

void play_game()
{
	game_init();
	
	while(1)
	{
		
		
	}
}
/*
 * void set_next()
 * Description: sets the rules of the game. Calculates what each point should be for the next step in the cycle. 
 */

void set_next()
{
	int i;
	int j;
	int alive_c[8][8];
	
	int next[8][8];
	//corners:
	//top left
	i = 0; j = 0;
	alive_c[i][j] = 0;
	if (the_matrix[i+1][j] != 0) 		//	xxx
		alive_c[i][j]++;				//	xxx
										//	xox		
	
	if (the_matrix[i+1][j+1] != 0) 		//	xxx
		alive_c[i][j]++;				//  xxx
										//	xxo
	if (the_matrix[i][j+1] != 0) 		//	xxx
		alive_c[i][j]++;				//	xxo
										//  xxx
	
	
	//bottom left
	i = 7; j = 0;
	alive_c[i][j] = 0;
	if (the_matrix[i][j+1] != 0) 		//	xxx
		alive_c[i][j]++;				//	xxo
										//  xxx
	
	if (the_matrix[i-1][j] != 0) 		//	xox
		alive_c[i][j]++;				//	xxx
										//  xxx
	
	if (the_matrix[i-1][j+1] != 0) 		//	xxo
		alive_c[i][j]++;				//	xxx
										//  xxx

	
	//top right
	i = 0; j = 7;
	alive_c[i][j] = 0;
	if (the_matrix[i+1][j-1] != 0) 		//	xxx
		alive_c[i][j]++;				//	xxx
										//  oxx
	
	if (the_matrix[i+1][j] != 0) 		//	xxx
		alive_c[i][j]++;				//	xxx
										//	xox	
	if (the_matrix[i][j-1] != 0) 		//	xxx
		alive_c[i][j]++;				//	oxx
										//  xxx
	//bottom right
	
	i = 7; j = 7;
	alive_c[i][j] = 0;
	if (the_matrix[i][j-1] != 0) 		//	xxx
		alive_c[i][j]++;				//	oxx
										//  xxx	
	
	if (the_matrix[i-1][j-1] != 0) 		//	oxx
		alive_c[i][j]++;				//	xxx
										//  xxx	
	
	if (the_matrix[i-1][j] != 0) 		//	xox
		alive_c[i][j]++;				//	xxx
										//  xxx
	
	for(j=1;j<7;j++)	
	{
		
		i=0;			//top row
		alive_c[i][j] = 0;
		if (the_matrix[i][j-1] != 0) 		//	xxx
			alive_c[i][j]++;				//	oxx
											//  xxx
		
		if (the_matrix[i][j+1] != 0) 		//	xxx
			alive_c[i][j]++;				//	xxo
											//  xxx
		
		if (the_matrix[i+1][j-1] != 0) 		//	xxx
			alive_c[i][j]++;				//	xxx
											//  oxx
		
		if (the_matrix[i+1][j] != 0) 		//	xxx
			alive_c[i][j]++;				//	xxx
											//	xox		
			
		if (the_matrix[i+1][j+1] != 0) 		//	xxx
			alive_c[i][j]++;				//  xxx
											//	xxo
		i=7;			//bottom row
		alive_c[i][j] = 0;
		if (the_matrix[i-1][j-1] != 0) 		//	oxx
			alive_c[i][j]++;				//	xxx
											//  xxx
		
		if (the_matrix[i-1][j] != 0) 		//	xox
			alive_c[i][j]++;				//	xxx
											//  xxx
		
		if (the_matrix[i-1][j+1] != 0) 		//	xxo
			alive_c[i][j]++;				//	xxx
											//  xxx
		
		if (the_matrix[i][j-1] != 0) 		//	xxx
			alive_c[i][j]++;				//	oxx
											//  xxx
		
		if (the_matrix[i][j+1] != 0) 		//	xxx
			alive_c[i][j]++;				//	xxo
											//  xxx
	}
	for(i = 1; i<7; i++)
	{
		j = 0;			//left column
		alive_c[i][j] = 0;
		if (the_matrix[i-1][j] != 0) 		//	xox
			alive_c[i][j]++;				//	xxx
											//  xxx
		
		if (the_matrix[i-1][j+1] != 0) 		//	xxo
			alive_c[i][j]++;				//	xxx
											//  xxx
		
		if (the_matrix[i][j+1] != 0) 		//	xxx
						alive_c[i][j]++;	//	xxo
											//  xxx
		
		if (the_matrix[i+1][j] != 0) 		//	xxx
			alive_c[i][j]++;				//	xxx
											//	xox		
			
		if (the_matrix[i+1][j+1] != 0) 		//	xxx
			alive_c[i][j]++;				//  xxx
											//	xxo
		
		j = 7;			//right column
		alive_c[i][j] = 0;
		if (the_matrix[i-1][j-1] != 0) 		//	oxx
			alive_c[i][j]++;				//	xxx
											//  xxx
		
		if (the_matrix[i-1][j] != 0) 		//	xox
			alive_c[i][j]++;				//	xxx
											//  xxx
		
		if (the_matrix[i][j-1] != 0) 		//	xxx
			alive_c[i][j]++;				//	oxx
											//  xxx
		if (the_matrix[i+1][j-1] != 0) 		//	xxx
			alive_c[i][j]++;				//	xxx
											//  oxx
		
		if (the_matrix[i+1][j] != 0) 		//	xxx
			alive_c[i][j]++;				//	xxx
											//	xox	
	}

	for(i = 1; i<7; i++)
	{
		for(j=1;j<7;j++)
		{
			alive_c[i][j] = 0;
		
			if (the_matrix[i-1][j-1] != 0) 		//	oxx
				alive_c[i][j]++;				//	xxx
												//  xxx
			
			if (the_matrix[i-1][j] != 0) 		//	xox
				alive_c[i][j]++;				//	xxx
												//  xxx
			
			if (the_matrix[i-1][j+1] != 0) 		//	xxo
				alive_c[i][j]++;				//	xxx
												//  xxx
			
			if (the_matrix[i][j-1] != 0) 		//	xxx
				alive_c[i][j]++;				//	oxx
												//  xxx
			
			if (the_matrix[i][j+1] != 0) 		//	xxx
				alive_c[i][j]++;				//	xxo
												//  xxx
			
			if (the_matrix[i+1][j-1] != 0) 		//	xxx
				alive_c[i][j]++;				//	xxx
												//  oxx
			
			if (the_matrix[i+1][j] != 0) 		//	xxx
				alive_c[i][j]++;				//	xxx
												//	xox		
				
			if (the_matrix[i+1][j+1] != 0) 		//	xxx
				alive_c[i][j]++;				//  xxx
												//	xxo
		}
	}
	
	//This loop sets the values in the next matrix according to the rules of the game.
	for(i=0;i<8;i++)
	{
		for(j=0;j<8;j++)
		{
			if(the_matrix[i][j] != 0) //rules 1,2,3
			{
				if(alive_c[i][j] < 2)
					next[i][j] = 0;
				else if(alive_c[i][j] == 2 || alive_c[i][j] == 3)
					next[i][j] = GREEN;
				else 
					next[i][j] = 0;
			}
			if(the_matrix[i][j] == 0) //rule 4
			{
				if(alive_c[i][j] == 3)
					next[i][j] = GREEN;
				else 
					next[i][j] = 0;
			}
		}
	}
	
	for(i = 0; i<8;i++)
	{
		for (j=0;j<8;j++)
		{
			the_matrix[i][j] = next[i][j];
		}
	}
	
	ledmatrix_write(the_matrix);	
}
/*void game_init() 
 * Description: initializes the pattern matrix of the game. 
 * 				Copies the initialized pattern into the main matrix 'the_matrix' and calls the qspi write function
 * 
 */

void game_init()
{
	int temp[8][8] =  {
			{	BLACK, 	RED, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK	},
			{	BLACK, 	BLACK, 	RED, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK	},
			{	RED, 	RED, 	RED, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK	},
			{	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK	},
			{	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK	},
			{	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK	},
			{	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK	},
			{	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK, 	BLACK	}};
	
	//copy temp into the_matrix
	int i;
	int j;
	for(i = 0; i<8; i++)
	{
		for(j=0;j<8;j++)
		{
			the_matrix[i][j] = temp[i][j];
		}
	}
	
	ledmatrix_write(the_matrix);
	
}
/*
 * void game_reset(): resets the initial pattern to all black (empty screen)
 */
void game_reset()
{
	//copy temp into the_matrix
	int i;
	int j;
	for(i = 0; i<8; i++)
	{
		for(j=0;j<8;j++)
		{
			the_matrix[i][j] = 0;
		}
	}
	
	// Write the matrix to the LED Matrix
	ledmatrix_write(the_matrix);
	
}